package nancal.mp.service.jenkins.publish

import nancal.mp.service.jenkins.JenkinsPublishJob


fun JenkinsPublishJob.f3_docker_push(): FunctionContent {
    var logFileName = ".ops.docker-build.result.txt"

    if (appDeploySetting.harborServer.host.isEmpty()) {
        return FunctionContent(
            "docker-push", """
echo "未设置 harborHost，忽略 docker push!"
"""
        );
    }

    return FunctionContent(
        "docker-push", """
echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "推送镜像:"
echo "${this.appDeploySetting.sudo} docker push ${appDeploySetting.harborServer.getHarborImagePrefix(this.task.language)}${this.task.buildInfo.serviceName}:＄docker_image_version"
            
${this.appDeploySetting.sudo} docker push ${appDeploySetting.harborServer.getHarborImagePrefix(this.task.language)}${this.task.buildInfo.serviceName}:＄docker_image_version
        
if [ ＄? -ne 0 ]; then
    echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '上传镜像错误!!!'
    show-file ${logFileName} 100
   
    exit 1
else
    echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '上传镜像成功!'
    show-file ${logFileName} 50
fi


if [ "＄git_commit_time" != "" ]; then
    echo "打标签(Git提交时间)并推送"
    echo "docker tag ${appDeploySetting.harborServer.getHarborImagePrefix(this.task.language)}${this.task.buildInfo.serviceName}:＄docker_image_version  ${appDeploySetting.harborServer.getHarborImagePrefix(this.task.language)}${this.task.buildInfo.serviceName}:git.＄git_commit_time"
        
    ${this.appDeploySetting.sudo} docker tag ${appDeploySetting.harborServer.getHarborImagePrefix(this.task.language)}${this.task.buildInfo.serviceName}:＄docker_image_version  ${appDeploySetting.harborServer.getHarborImagePrefix(this.task.language)}${this.task.buildInfo.serviceName}:git.＄git_commit_time
    
    echo "${this.appDeploySetting.sudo} docker push  ${appDeploySetting.harborServer.getHarborImagePrefix(this.task.language)}${this.task.buildInfo.serviceName}:git.＄git_commit_time"
    ${this.appDeploySetting.sudo} docker push  ${appDeploySetting.harborServer.getHarborImagePrefix(this.task.language)}${this.task.buildInfo.serviceName}:git.＄git_commit_time
    
    if [ ＄? -ne 0 ]; then
        echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '上传Git提交时镜像错误!!!'
        show-file ${logFileName} 20
       
        exit 1
    else
        echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '上传Git提交时镜像成功!'
        show-file ${logFileName} 10
    fi
fi

"""
    )
}